<template>
  <el-container>
    <el-dialog
      :title="title"
      :visible.sync="isShowDialog"
    >
      <el-form ref="setFrom" :model="roleInfo" :rules="rules">
        <el-form-item prop="name" label="角色名称">
          <el-input v-model="roleInfo.name" autocomplete="off" />
        </el-form-item>
        <el-form-item prop="description" label="角色描述">
          <el-input v-model="roleInfo.description" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="isShowDialog = false">取 消</el-button>
        <el-button
          type="primary"
          @click="updateRoleInfo"
        >确 定</el-button>
      </div>
    </el-dialog>
  </el-container>

</template>

<script>
import { getRoleDetail, updateRoleInfo, addRole } from '@/api/roles'
export default {
  props: {
    title: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      isShowDialog: false,
      roleInfo: { name: '', description: '' },
      rules: {
        name: [
          { required: true, message: '角色名称没有填入', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述没有填入', trigger: 'blur' },
          { min: 5, max: 200, message: '请输入 5~200 个字角色描述', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async updateRoleInfo() {
      await this.$refs.setFrom.validate()
      if (this.roleInfo.id) {
        await updateRoleInfo(this.roleInfo)
        this.$message({
          message: '修改成功',
          type: 'success'
        })
      } else {
        await addRole(this.roleInfo)
        this.$message({
          message: '新增成功',
          type: 'success'
        })
      }
      this.$refs.setFrom.resetFields()
      this.isShowDialog = false
      this.$emit('updateRole')
    },
    async getRolesDetailInfo(id) {
      this.roleInfo = await getRoleDetail(id)
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog .el-dialog__header {
  background-color: rgb(100, 159, 222);
  span {
    color: #fff;
  }
}
</style>
